package dk.itu.KrakMap;

/**
 * Interval to represent a span between two numbers, and calculate related
 * values. The interval is inclusive, meaning that values that are equal to the
 * end points of the interval is also included.
 * @author ITU-KF04-2011, Group 11
 */
public class Interval {

    public final double low;
    public final double high;

    /**
     * Create a new interval between two values
     * @param low The lowest of the two values
     * @param high The highest of the two values
     */
    public Interval(double low, double high) {
        if (high < low) {
            throw new RuntimeException("Low value must be lower than high value in Interval");
        }
        this.low = low;
        this.high = high;
    }

    /**
     * Does the interval contain the given value?
     * @param x The given value
     * @return boolean value
     */
    public boolean contains(double x) {
        return x >= low && high >= x;
    }

    /**
     * Does the Interval intersect with the given Interval?
     * @param interval The given interval
     * @return boolean value
     */
    public boolean intersects(Interval interval) {
        if (this.high < interval.low) {
            return false;
        }
        if (interval.high < this.low) {
            return false;
        }
        return true;
    }

    /**
     * Get the size of the span of the interval
     * @return The size of the span of the interval as a double
     */
    public double spanSize() {
        return high - low;
    }

    @Override
    public String toString() {
        return "[" + low + ", " + high + "]";
    }
}
